﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;
using FX.CC.Andon.Entity.Service;

namespace FX.CC.Andon.Service.Service
{
    public class KanbanService : DbHelperBase
    {
        public IList<KanbanWrapper> GetEvent(Guid sessionID, DateTime bizDate)
        {
            string sql =
@"SELECT   Rec.EventID, WS.WSName, Ln.LineName, Eq.EqCode, Eq.EqName, EType.ETypeName, 
                Detail.DetailName, Rec.StartTime, OpUsr.FullName AS OpUserName, OpUsr.MobileNo AS OpMobileNo, 
                ISNULL(Rec.SignUserName, N'') AS SignUserName, ISNULL(Rec.RspTime, 0) AS RspTime, Rec.ReduceMin
FROM      (SELECT   EventID, EqID, ETypeCode, DetailID, TaskID, StartTime, StartUserID, EndUserID, SignUserName, Remark, RspTime, ReduceMin
                 FROM UT_EventRec
                 WHERE   (EndTime IS NULL) AND (BizDate = @bizDate)) AS Rec INNER JOIN
                UT_Equipment AS Eq ON Rec.EqID = Eq.EqID INNER JOIN
                UT_Line AS Ln ON Eq.LineID = Ln.LineID INNER JOIN
                    (SELECT   LineID FROM UT_ListeningLine WHERE SessionID = @sid) AS LsLN ON Ln.LineID = LsLN.LineID INNER JOIN
                UT_WorkShop AS WS ON Ln.WSID = WS.WSID INNER JOIN
                UT_ETypeDetail AS Detail ON Rec.DetailID = Detail.DetailID INNER JOIN
                    (SELECT   ETypeCode FROM UT_ListeningError WHERE SessionID = @sid) AS LsErr ON Rec.ETypeCode = LsErr.ETypeCode INNER JOIN
                UT_User AS OpUsr ON Rec.StartUserID = OpUsr.UserID INNER JOIN
                UT_EType AS EType ON Rec.ETypeCode = EType.ETypeCode;";

            IList<KanbanWrapper> list = new List<KanbanWrapper>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                Db.AddInParameter(cmd, "@sid", DbType.Guid, sessionID);
                Db.AddInParameter(cmd, "@bizDate", DbType.DateTime, bizDate);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var obj = new KanbanWrapper();
                            obj.EventID = ReaderHelper.GetValue<Guid>(reader, "EventID");
                            obj.WSName = ReaderHelper.GetValue<string>(reader, "WSName");
                            obj.LineName = ReaderHelper.GetValue<string>(reader, "LineName");
                            obj.EqCode = ReaderHelper.GetValue<string>(reader, "EqCode");
                            obj.EqName = ReaderHelper.GetValue<string>(reader, "EqName");
                            obj.ETypeName = ReaderHelper.GetValue<string>(reader, "ETypeName");
                            obj.DetailName = ReaderHelper.GetValue<string>(reader, "DetailName");
                            obj.StartTime = ReaderHelper.GetValue<DateTime>(reader, "StartTime");
                            obj.OpUserName = ReaderHelper.GetValue<string>(reader, "OpUserName");
                            obj.OpMobileNo = ReaderHelper.GetValue<string>(reader, "OpMobileNo");
                            obj.SignUserName = ReaderHelper.GetValue<string>(reader, "SignUserName");
                            obj.RspTime = ReaderHelper.GetValue<decimal>(reader, "RspTime");
                            obj.ReduceMin = ReaderHelper.GetValue<int>(reader, "ReduceMin");
                            list.Add(obj);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }
        }

        public IList<TaskKanbanWrapper> GetTaskListBySession(Guid sessionID, DateTime bizDate)
        {
            string sql =
@"SELECT   Rec.BizDate, Rec.TaskID, WS.WSName, Ln.LineName, Eq.EqCode, Eq.EqName, Prd.PrdCode, Prd.PrdName, Rec.PlanQty, 
                Rec.ConfirmQty, Rec.StartTime, Rec.ProcessIdx
FROM      (SELECT   Tsk.TaskID, Tsk.FlowID, Flw.PrdID, Tsk.BizDate, Tsk.ShiftID, Tsk.EqID, Tsk.PlanQty, Tsk.ConfirmQty, Tsk.PlanStartTime, 
                                 Tsk.PlanEndTime, Tsk.ErrorRemark, Tsk.RecStatus, Flw.ProcessIdx, TskRec.StartTime
                 FROM      UT_TaskRec AS TskRec INNER JOIN
                                 UT_PrdTask AS Tsk ON TskRec.TaskID = Tsk.TaskID
                                 inner join UT_Flow Flw on Tsk.FlowID = Flw.FlowID
                 WHERE   (TskRec.EndTime IS NULL) AND (Tsk.BizDate = @bizDate)) AS Rec INNER JOIN
                UT_Product AS Prd ON Rec.PrdID = Prd.PrdID INNER JOIN
                UT_Equipment AS Eq ON Rec.EqID = Eq.EqID INNER JOIN
                UT_Line AS Ln ON Eq.LineID = Ln.LineID INNER JOIN
                    (SELECT   LineID FROM UT_ListeningLine WHERE   (SessionID = @sid)) AS LsLN ON Ln.LineID = LsLN.LineID INNER JOIN
                UT_WorkShop AS WS ON Ln.WSID = WS.WSID;";

            IList<TaskKanbanWrapper> list = new List<TaskKanbanWrapper>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                Db.AddInParameter(cmd, "@sid", DbType.Guid, sessionID);
                Db.AddInParameter(cmd, "@bizDate", DbType.DateTime, bizDate);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var obj = new TaskKanbanWrapper();
                            obj.TaskTID = ReaderHelper.GetValue<Guid>(reader, "TaskID");
                            obj.WSName = ReaderHelper.GetValue<string>(reader, "WSName");
                            obj.LineName = ReaderHelper.GetValue<string>(reader, "LineName");
                            obj.EqCode = ReaderHelper.GetValue<string>(reader, "EqCode");
                            obj.EqName = ReaderHelper.GetValue<string>(reader, "EqName");
                            obj.PrdCode = ReaderHelper.GetValue<string>(reader, "PrdCode");
                            obj.PrdName = ReaderHelper.GetValue<string>(reader, "PrdName");
                            obj.PlanQty = ReaderHelper.GetValue<int>(reader, "PlanQty");
                            obj.ConfirmQty = ReaderHelper.GetValue<int>(reader, "ConfirmQty");
                            obj.StartTime = ReaderHelper.GetValue<DateTime>(reader, "StartTime");
                            obj.ProcessIdx = ReaderHelper.GetValue<int>(reader, "ProcessIdx");
                            obj.PrdCode += ("-M" + obj.ProcessIdx);
                            obj.PrdName += ("-M" + obj.ProcessIdx);
                            list.Add(obj);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }
        }

        public IList<PrdTaskWrapper> GetTaskList(Guid eqID, DateTime bizDate) 
        {
            string sql =
    @"SELECT   Tsk.TaskID, Flw.PrdID, Prd.PrdCode, Prd.PrdName, SH.ShiftName, Tsk.PlanQty, Tsk.PlanStartTime, Tsk.PlanEndTime, Flw.ProcessIdx
FROM      UT_PrdTask AS Tsk INNER JOIN
                UT_Shift AS SH ON Tsk.ShiftID = SH.ShiftID INNER JOIN
                UT_Flow as Flw on Tsk.FlowID = Flw.FlowID inner join
                UT_Product AS Prd ON Flw.PrdID = Prd.PrdID
WHERE   (Tsk.BizDate = @bizDate) AND (Tsk.EqID = @eqID) AND (Tsk.RecStatus = 1)
ORDER BY Tsk.ConfirmQty, Tsk.PlanStartTime;";

            IList<PrdTaskWrapper> list = new List<PrdTaskWrapper>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                Db.AddInParameter(cmd, "@eqID", DbType.Guid, eqID);
                Db.AddInParameter(cmd, "@bizDate", DbType.DateTime, bizDate);
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var obj = new PrdTaskWrapper();
                            obj.TaskID = ReaderHelper.GetValue<Guid>(reader, "TaskID");
                            obj.ShiftName = ReaderHelper.GetValue<string>(reader, "ShiftName");
                            obj.PrdID = ReaderHelper.GetValue<Guid>(reader, "PrdID");
                            obj.PrdCode = ReaderHelper.GetValue<string>(reader, "PrdCode");
                            obj.PrdName = ReaderHelper.GetValue<string>(reader, "PrdName");
                            obj.PlanQty = ReaderHelper.GetValue<int>(reader, "PlanQty");
                            obj.PlanStartTime = ReaderHelper.GetValue<DateTime>(reader, "PlanStartTime");
                            obj.PlanEndTime = ReaderHelper.GetValue<DateTime>(reader, "PlanEndTime");
                            obj.ProcessIdx = ReaderHelper.GetValue<int>(reader, "ProcessIdx");
                            obj.PrdCode += ("-M" + obj.ProcessIdx);
                            obj.PrdName += ("-M" + obj.ProcessIdx);
                            list.Add(obj);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }
        }

        public IList<string> GetNotice()
        {
            string sql = "SELECT NoticeTitle FROM UT_Notice WHERE (Flag = 1) ORDER BY InputTime DESC;";

            IList<string> list = new List<string>();
            using (var cmd = Db.GetSqlStringCommond(sql))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            string noticeStr = ReaderHelper.GetValue<string>(reader, "NoticeTitle");
                            list.Add(noticeStr);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return list;
            }
        }
    }
}
